#include "signals.h"
#define CHECK_NULL_IO_POINTERS(i,o) \
	if(!i||!i->signal||!o||!(o)->signal)\
	{\
		return NULL_PARAMETERS;\
	}\

#define CHECK_NULL_POINTERS(i,im,o) \
	if(!i||!i->signal||!im||!im->signal||!o||!(o)->signal)\
	{\
		return NULL_PARAMETERS;\
	}\

int convolution(SignalStr* input,SignalStr* impulseResponse,SignalStr* output)
{
	int i,j;
	CHECK_NULL_POINTERS(input,impulseResponse,output)
	for(i=0;i<input->length;i++)
	{
		for(j=0;j<impulseResponse->length;j++)
		{
			(output)->signal[i+j]+= input->signal[i]*impulseResponse->signal[j];
		}
	}
	return OK;
}
int decompose(SignalStr* input,SignalStr* output)
{
	int i;
	CHECK_NULL_IO_POINTERS(input,output);
	if(output->length != input->length*input->length)
	{
		return BUFFER_WRONG_SIZE;
	}
	for(i=0;i<input->length;i++)
	{
		output->signal[input->length*i+i]=input->signal[i];
	}
	return OK;
}
int synthesize(SignalStr* input,int originalLength)
{
	int i,j;
	if(!input||!input->signal)
	{
		return NULL_PARAMETERS;
	}
	for(i=0;i<originalLength;i++)
	{
		for(j=0;j<originalLength;j++)
		{
			input->signal[i]+= input->signal[i+j*originalLength];
		}
	}
	input->length=originalLength;
	return OK;
}
